package models

import "go-admin/utils"

type IPInternetPublic struct {
	BaseModel
	IPAddress     string `gorm:"column:ip_address;type:varchar(50);unique;not null;" json:"ip_address"` //IP地址
	User          string `gorm:"column:user;type:varchar(50)" json:"user"`                              //使用人
	PcType        string `gorm:"column:pc_type; type:varchar(100)" json:"pc_type"`                      //设备类型
	Position      string `gorm:"column:position; type:varchar(100)" json:"position"`                    //设备位置
	Brand         string `gorm:"column:brand; type:varchar(100)" json:"brand"`                          //品牌
	PcModel       string `gorm:"column:pc_model; type:varchar(100)" json:"pc_model"`                    //型号
	OperateSystem string `gorm:"column:operate_system; type:varchar(100)" json:"operate_system"`        //操作系统
	MacAdress     string `gorm:"column:mac_address; type:varchar(100)" json:"mac_address"`              //设备物理地址
	DiskSN        string `gorm:"column:disk_sn; type:varchar(100)" json:"disk_sn"`                      //硬盘序列号
	Department    string `gorm:"column:department; type:varchar(100)" json:"department"`                //使用部门
	Remarks       string `gorm:"column:remarks; type: varchar(255)" json:"remarks"`                     //备注
}

func (table *IPInternetPublic) TableName() string {
	return "ip_internet_public"
}

func (ip *IPInternetPublic) IPExist() bool {
	var count int64
	if ip.ID != 0 {
		DB.Model(ip).Where("id != ? AND ip_address = ?", ip.ID, ip.IPAddress).Count(&count)
	} else {
		DB.Model(ip).Where("ip_address = ?", ip.IPAddress).Count(&count)
	}
	return count > 0
}

// 创建IP地址 1-253  254为网关
func (ip *IPInternetPublic) SaveIP() error {

	return DB.Create(ip).Error
}

// 获取列表
func (ip *IPInternetPublic) GetUserList(q utils.QueryRequest) ([]*IPInternetPublic, int64, error) {
	var (
		count int64
		list  = make([]*IPInternetPublic, 0)
	)
	tx := DB.Model(ip).Select("id, ip_address,user,pc_type,position,brand,pc_model,operate_system,mac_address,disk_sn,department, created_at, updated_at")
	if q.Keyword != "" {
		tx.Where("user LIKE ?", "%"+q.Keyword+"%")
	}

	err := tx.Scopes(q.Paginate()).Count(&count).Find(&list).Error

	return list, count, err
}
